package com.yangjie.user.dao;



import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.yangjie.common.dao.HibernateDao;
import com.yangjie.user.domain.User;

@Component
public class UserDao extends HibernateDao<User>{
	Logger logger = Logger.getLogger(getClass());
	
	public List<User> findAllNonAdminUsers() {
		Query query = (Query)getSession().createQuery("SELECT T FROM " + getDomainClass().getName() + " T WHERE T.username != 'admin'");
		List<User> userList = query.list();
		return userList;
	}
	public User findUserByUsername(String username) {
		logger.debug("findUserByUsername:" + username);
		Query query = (Query)getSession().createQuery("SELECT T FROM " + getDomainClass().getName() + " T WHERE T.username=:username");
		query.setParameter("username", username);
		List<User> userList = query.list();
		if(userList.size() > 0) {
			User user = (User)query.list().get(0);
			logger.debug(user);
			return user;
		}
		
		return null;
	}
	
	@Override
	public void update(User user) {
		Query query = (Query)getSession().createSQLQuery("UPDATE USER  SET username=:username, email=:email, description=:description, mobile=:mobile where id=:id");
		query.setParameter("username", user.getUsername());
		query.setParameter("email", user.getEmail());
		query.setParameter("description", user.getDescription());
		query.setParameter("mobile", user.getMobile());
		query.setParameter("id", user.getId());
		query.executeUpdate();

	}
	
}
